#ifndef Q4_H
#define Q4_H
#include <cmath>
#include <numeric>
#include <stdexcept>
#include <vector>
namespace q4 {
    struct Vector2D
    {
        double x{};
        double y{};
    };
    struct Sensor
    {
    	Vector2D pos;
    	double accuracy;    
    };
    inline Vector2D kalman_filter(std::vector<Sensor> sensors) {
        double accu_sum = std::accumulate(sensors.begin(),sensors.end(),0.f,
            [](const double a , const Sensor & b) { return a + b.accuracy; } );
        if( std::fabs( accu_sum - 0.f ) < 1e-10 )
            throw std::logic_error("accu_num should not be zero");
        Vector2D vinit;
        vinit.x = 0.f;
        vinit.y = 0.f;
        Vector2D ret = std::accumulate( sensors.begin() , sensors.end() ,vinit,
            [accu_sum](const Vector2D a , const Sensor & b) 
            {   
                Vector2D ret;
                ret.x = a.x + b.pos.x * ( b.accuracy /accu_sum );
                ret.y = a.y + b.pos.y * ( b.accuracy /accu_sum );
                return ret;
            } );
        return ret;
    }

}

#endif //Q4_H